//
//  TETestViewController.m
//  TintExample
//
//  Created by Kevin Wu on 9/6/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TETestViewController.h"


@implementation TETestViewController


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        
        _queue = [[NSOperationQueue alloc] init];
        [_queue setMaxConcurrentOperationCount:2];
        
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}


#pragma mark - UIViewController

- (void)loadView
{
    [super loadView];
    
    [self addButtonWithTitle:@"start" selector:@selector(doit1:)];
    [self addButtonWithTitle:@"cancel" selector:@selector(doit2:)];
    [self addButtonWithTitle:@"show" selector:@selector(doit3:)];
    
}


- (void)doit1:(id)sender
{
    
//    NSString *origin = @"laksjdfo lasdj a safl";
//    NSData *dt = [origin dataUsingEncoding:NSUTF8StringEncoding];
//    NSString *result = [dt newStringWithBase64EncodedString];
//    NSLog(@"HH%@HH", result);
    
    NSString *origin = @"bGFrc2pkZm8gbGFzZGogYSBzYWZs";
    NSData *dt = [NSData newDataByDecodeBase64String:origin];
    NSString *string = [[NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
    NSLog(@"HH%@HH", string);
    
    
//    _request1 = [[TTHTTPRequest alloc] initWithAddress:@"http://www.facebook.com/"];
//    _request2 = [[TTHTTPRequest alloc] initWithAddress:@"http://farm6.staticflickr.com/5238/7110620063_76d694a3e6_k.jpg"];
//    _request3 = [[TTHTTPRequest alloc] initWithAddress:@"http://www.youtube.com/"];
//    _request4 = [[TTHTTPRequest alloc] initWithAddress:@"http://www.baidu.com/"];
//    _request5 = [[TTHTTPRequest alloc] initWithAddress:@"http://www.twitter.com/"];
//    
//    
//    [_request1 addDelegate:self];
//    [_request2 addDelegate:self];
//    [_request3 addDelegate:self];
//    [_request4 addDelegate:self];
//    [_request5 addDelegate:self];
//    
//    
//    [(TTHTTPRequest *)_request1 startAsynchronous];
//    [(TTHTTPRequest *)_request2 startAsynchronous];
//    [(TTHTTPRequest *)_request3 startAsynchronous];
//    [(TTHTTPRequest *)_request4 startAsynchronous];
//    [(TTHTTPRequest *)_request5 startAsynchronous];
//    
//    [self doit3:nil];
}

- (void)doit2:(id)sender
{
    [_request5 cancel];
    [_request4 cancel];
    [_request3 cancel];
    [_request2 cancel];
    [_request1 cancel];
    
    [self doit3:nil];
}

- (void)doit3:(id)sender
{
    NSLog(@"\n\n");
    NSArray *array = [[TTHTTPRequest requestOperationQueue] operations];
    NSLog(@"%@", array);
    for ( id object in array ) {
        if ( object == _request1 ) NSLog(@"request1");
        if ( object == _request2 ) NSLog(@"request2");
        if ( object == _request3 ) NSLog(@"request3");
        if ( object == _request4 ) NSLog(@"request4");
        if ( object == _request5 ) NSLog(@"request5");
    }
    
    NSLog(@"\n\n");
}


- (void)operationDidStart:(TTOperation *)operation
{
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    NSLog(@"start: %@", request.address);
}

- (void)operationDidFail:(TTOperation *)operation withError:(NSError *)error
{
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    NSLog(@"failed: %@", request.address);
}

- (void)operationDidFinish:(TTOperation *)operation
{
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    NSLog(@"finished: %@", request.address);
}


@end
